/*
 *
 *  * COPYRIGHT China Mobile (SuZhou) Software Technology Co.,Ltd. 2017
 *  *
 *  * The copyright to the computer program(s) herein is the property of
 *  * CMSS Co.,Ltd. The programs may be used and/or copied only with written
 *  * permission from CMSS Co.,Ltd. or in accordance with the terms and conditions
 *  * stipulated in the agreement/contract under which the program(s) have been
 *  * supplied.
 *
 */

package cn.wxManage.goodsManage;

import cn.common.common.model.ServerResponse;
import cn.wxManage.common.util.RedisUtils;
import cn.wxManage.user.model.UserInfo;
import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by  on 2017/5/22.
 */
@Component
@Aspect
public class GoodsAop {
    @Autowired
    RedisUtils redisUtils;

    @Pointcut("execution(* cn.wxManage.goodsManage.controller..*.*(..))")
    public void webLog(){

    }

    @Before("webLog()")
    public void doBeforeAdvice(){
    }

    @After("webLog()")
    public void doAfterReturning() throws Throwable {
    }

    @Around("webLog()")
    public Object Interceptor(ProceedingJoinPoint pjp) throws Throwable {
        ServletRequestAttributes attributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String sessionKey = request.getHeader("cookie");
        if (sessionKey == null){
            return ServerResponse.createByErrorMessage("请提供会话信息");
        }
        Object json = redisUtils.get(sessionKey);
        if (json == null){
            return ServerResponse.createByErrorLogin();
        }
        request.setAttribute("userInfo", JSONObject.parseObject(json.toString(),UserInfo.class));
        return pjp.proceed();
    }
}
